package com.hospitalsystem.service.impl;

import com.hospitalsystem.dao.MedicalRecordDao;
import com.hospitalsystem.dao.PatientDao;
import com.hospitalsystem.dao.PatientRecordDao;
import com.hospitalsystem.dao.RefundRecordDao;
import com.hospitalsystem.entity.*;
import com.hospitalsystem.service.MedicalRecordService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (MedicalRecord)表服务实现类
 *
 * @author makejava
 * @since 2024-12-28 19:32:48
 */
@Service("medicalRecordService")
public class MedicalRecordServiceImpl implements MedicalRecordService {
    @Resource
    private MedicalRecordDao medicalRecordDao;
    @Resource
    private PatientRecordDao patientRecordDao;
    @Resource
    private PatientDao patientDao;
    @Resource
    private RefundRecordDao refundRecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param mId
     * @return 实例对象
     */
    @Override
    public MedicalRecord queryById(String mId) {
        return this.medicalRecordDao.queryById(mId);
    }

    /**
     * 分页查询
     *
     * @param medicalRecord 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<MedicalRecord> queryByPage(MedicalRecord medicalRecord, PageRequest pageRequest) {
        long total = this.medicalRecordDao.count(medicalRecord);
        return new PageImpl<>(this.medicalRecordDao.queryAllByLimit(medicalRecord, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param medicalRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MedicalRecord insert(MedicalRecord medicalRecord) {
        this.medicalRecordDao.insert(medicalRecord);
        return medicalRecord;
    }

    /**
     * 修改数据
     *
     * @param medicalRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MedicalRecord update(MedicalRecord medicalRecord) {
        this.medicalRecordDao.update(medicalRecord);
        return this.queryById(medicalRecord.getMId());
    }

    /**
     * 通过主键删除数据
     *
     * @param mId
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String mId) {
        return this.medicalRecordDao.deleteById(mId) > 0;
    }

    @Override
    public List<Map<String,Object>> getMedicalRecord() {
        List<MedicalRecord> medicalRecords = this.medicalRecordDao.getMedicalRecord();
        List<Map<String,Object>> resutlMaps = new ArrayList<>();

//        MedicalRecordComparator comparator = new MedicalRecordComparator();
//        medicalRecords.sort(comparator);


        Map<String,Integer> map = new HashMap<>();
        List<RefundRecord> refundRecords = this.refundRecordDao.getAllRefundRecords();
        for(RefundRecord refundRecord:refundRecords){
            if(map.containsKey(refundRecord.getMId())) {
                continue;
            }else{
                map.put(refundRecord.getMId(),1);
            }
        }

        for(MedicalRecord medicalRecord:medicalRecords){
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("mid",medicalRecord.getMId());
//            PatientRecord patientRecord = this.patientRecordDao.queryById(medicalRecord.getPRId());
//            Patient patient = this.patientDao.queryById(patientRecord.getPId());
//            resultMap.put("name",patient.getName());
            resultMap.put("drugs",medicalRecord.getDrugList());
//            resultMap.put("ifTake",medicalRecord.getIfTake());
            if(map.containsKey(medicalRecord.getMId())){
                resultMap.put("ifTake","-1");
            }
            else{
                resultMap.put("ifTake",medicalRecord.getIfTake());
            }
            resutlMaps.add(resultMap);
        }

        return resutlMaps;
    }

    @Override
    public List<Map<String, Object>> getMedicalRecordById(String pid) {
        List<MedicalRecord> medicalRecords = new ArrayList<>();

        //谬误
        //medicalRecords = this.medicalRecordDao.getMedicalRecordsOfOnePatient(pid);

        List<PatientRecord> patientRecords = new ArrayList<>();
        patientRecords = this.patientRecordDao.getPatientRecordsOfOnePatient(pid);
        for (PatientRecord patientRecord : patientRecords) {
            MedicalRecord medicalRecord = this.medicalRecordDao.getMedicalRecordByPrid(patientRecord.getPRId());
            medicalRecords.add(medicalRecord);
        }

        List<Map<String, Object>> medicalMaps = new ArrayList<>();

        for (MedicalRecord medicalRecord : medicalRecords) {
            Map<String, Object> medicalMap = new HashMap<>();
            medicalMap = medicalRecord.convertToChargeMap();

            double drugPrice = 0;
            double checkPrice = 0;
            //计算药费
            if (!medicalRecord.getDrugList().equals("")) {
                System.out.println(medicalRecord.getDrugList());
                List<Map<String,Object>> drugMaps = new ArrayList<>();
                String[] drugs = medicalRecord.getDrugList().split("\n");
                for (String drug : drugs) {
                    String parts[] = drug.split("\\s");
                    Drug drugInfo = this.medicalRecordDao.getDrugByName(parts[0]);
                    try {
                        drugPrice += drugInfo.getPrice() * Integer.parseInt(parts[1]);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Map<String,Object> drugmap = new HashMap<>();
                    drugmap.put(parts[0],parts[1]);
                    drugMaps.add(drugmap);
                }
                medicalMap.put("drugs",drugMaps);
            }
            //计算检查费
            if (!medicalRecord.getCheckList().equals("")) {
                List<Map<String,Object>> checkMaps = new ArrayList<>();
                String[] checks = medicalRecord.getCheckList().split("\n");
                for (String check : checks) {
                    CheckList checkInfo = this.medicalRecordDao.getCheckByName(check);
                    checkPrice += checkInfo.getPrice();
                    Map<String,Object> checkMap = new HashMap<>();
                    checkMap.put(check,1);
                    checkMaps.add(checkMap);
                }
                medicalMap.put("checks",checkMaps);
            }
            medicalMap.put("drugPrice", drugPrice);
            medicalMap.put("checkPrice", checkPrice);
            medicalMap.put("sumPrice", drugPrice + checkPrice);
            medicalMaps.add(medicalMap);
        }
        return medicalMaps;
    }
}
